<?php
/**
 * 图百科管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-31
 */
namespace Admin\Controller;

class WikiController extends CommonController{
    /*
     * 构造函数
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db   =new \Model\WikiModel();
    }
    
    /*
     * 涂百科列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.name")){
            $map['name']=I("request.name");
        }
        if(I("request.company")){
            $map['company'] =I("request.company");
        }
        $map        =$this->_search("Wiki");
        //$sortBy     ='top';
        //$sort       =desc;
        $wikiList   =$this->_list($this->db,$map,'id',FALSE);
        $this->assign("wikiList",$wikiList);
        $this->display();
    }
    
    /*
     * 显示编辑涂百科
     * 
     * @return #
     */
    public function show(){
        if(I("request.id")){
            $wikiInfo   =$this->db->find(I("request.id"));
            $this->assign("wikiInfo",$wikiInfo);
        }
        $this->display();
    }
    
    /*
     * 编辑涂百科
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']      =I("request.id","","int");
        }
        if(I("request.name")){
            $map['name']    =I("request.name","","trim");
        }else{
            $this->error("名称不能为空");
        }
        if(I("request.icon")){
            $map['icon']=I("request.icon","","trim");
        }
        if(I("request.type")){
            $map['type']    =I("request.type","","trim");
        }
        if(I("request.typeNo")){
            $map['typeNo']  =I("request.typeNo","","trim");
        }
        if(I("company")){
            $map['company'] =I("request.company","","trim");
        }
        if(I("website")){
            $map['website'] =I("request.website","","trim");
        }
        if(I("request.price")){
            $map['price']   =I("request.price","","trim");
        }
        if(I("request.description")){
            $map['description'] =I("request.description","","trim");
        }
        if(I("request.top")){
            $map['top_sign']    =1;
        }
        if(I("request.brand")){
            $map['brand']       =I("request.brand");
        }            
        if($this->db->edit($map)){
            $this->success("涂百科编辑成功");
        }else{
            $this->error("涂百科编辑失败");
        }
    }
    
    /*
     * 删除、批量删除涂百科
     * 
     * @return #
     */
    public function del(){
        $map['id']      =I("request.id");
        if($this->db->del($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
   
    /*
     * 显示图片上传
     * 
     * @return #
     */
    public function photo(){
        if(!I("request.id")){
            $this->error("参数错误");
        }
        $_SESSION['wiki_image']=I("request.id");
        $this->display();
    }
    
    /*
     * 上传图百科
     * 
     * @return #
     */
    public function wikiphoto(){
        if(!empty($_FILES['images']['name'])){            
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',                              //根目录
                'savePath'  =>'wiki/',                                            //保存路径
                'maxSize'   =>'902400',                                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['images']);
            if(!$upload){
                echo $new_upload->getError();
            }else{
                echo $upload['savepath'].$upload['savename'];
            }           
        }
    }
    
    /*
     * 显示数据库导入
     * 
     * @return #
     */
    public function addExcel(){
        $this->display();
    }
    
    /*
     * 处理excel上传导入数据库
     * 
     * @return #
     */
    public function wikiExcel(){        
        if(!empty($_FILES['excel']['name'])){            
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',       //根目录
                'savePath'  =>'wiki/',                              //保存路径
                'maxSize'   =>'902400',                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('xls','xlsx')                   //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['excel']);
            if(!$upload){
                echo $new_upload->getError();
            }else{
                
                $file='./Application/Public/upload/'.$upload['savepath'].$upload['savename'];


                //show_bug($upload);exit;

                Vendor("PHPExcel.PHPExcel.IOFactory");

                $objReader  =\PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel= $objReader -> load($file,$encode="utf-8");

                $objPHPExcel->setActiveSheetIndex(0);
                $sheet1 = $objPHPExcel->getSheet(0);
                //获取行数，并把数据读取出来$data数组
                $rowCount = $sheet1->getHighestRow();//excel行数
                $data = array();
                
                for ($i = 2; $i <= $rowCount; $i++){
                    $item['name']       =$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                    $item['brand']      =$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                    $item['icon']       =$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                    $item['type']       =$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                    $item['typeNo']     =$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                    $item['company']    =$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                    $item['website']    =$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                    $item['price']      =$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
                    $item['description']=$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
                    $data[] = $item;
                } 
                $sum = count($data);
                foreach($data as $k=>$v){
                    if(!$this->db->add($v)){
                       echo "文件错误";
                    }
                }
                echo "导入成功，请刷新页面";
            }
        }
    }
}